package com.maxima.maximaAttendance.daos;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.maxima.maximaAttendance.pojos.User;
import com.maxima.maximaAttendance.utils.HibernateUtils;

public class UserDAO {
	
	Map<String, Object> map;
	
	public User add(User u) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.save(u);
		session.getTransaction().commit();
		session.close();
		/*map.put("usersList", new UserList().initUserList());
		this.setSession(map);*/
		return u;
	}
//	public void setStatus(Integer id,boolean status) {
//		Session session = HibernateUtils.getSession();
//		session.beginTransaction();
//		User user = null;
//		try {
//			user = (User)session.createQuery("select u from User u where u.id="+ id).uniqueResult();
//			user.setLogged_in(status);
//		} catch (HibernateException e) {
//			e.printStackTrace();
//			session.getTransaction().rollback();
//		}
//		session.getTransaction().commit();
//		session.close();
//	}
	public void changePassword(Integer id,String password) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			user = (User)session.createQuery("select u from User u where u.id="+ id).uniqueResult();
			user.setPassword(password);
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}
	

	@SuppressWarnings("unchecked")
	public List<User> getAllUsers()
	{
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<User> users = null;
		try {
			users = (List<User>)session.createQuery("select u from User u order by u.firstname").list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getAllProjectNames()
	{
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<String> users = null;
		try {
			
			users = (List<String>)session.createQuery("select distinct u.project from User u order by u.project").list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}
	
	public User getUser(Integer id)
	{
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			
			user = (User)session.createQuery("select u from User u where u.id="+ id).uniqueResult();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return user;
	}
	public void update(Integer id,User u) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			user = (User)session.createQuery("select u from User u where u.id="+ id).uniqueResult();
//			user.setFirstname(u.getFirstname());
//			user.setLastname(u.getLastname());
//			user.setEmp_id(u.getEmp_id());
			user.setPassword(u.getPassword());
			user.setProject(u.getProject());
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}
	
}
